package com.zhangxx.inventory.thread.request;

import com.zhangxx.common.exception.ExceptionCast;
import com.zhangxx.common.response.resultCode.CommonCode;
import com.zhangxx.inventory.pojo.ProductInventory;
import com.zhangxx.inventory.service.ProductInventoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description :  清空缓存，修改数据库
 * @Author : leizhang
 * @Date 14:56 2022/3/18
 **/
public class ProductInventoryDBUpdateRequest implements Request {
    private Logger logger = LoggerFactory.getLogger(ProductInventoryDBUpdateRequest.class);
    /**
     * @Description : 商品库存
     **/
    private ProductInventory productInventory;

    private ProductInventoryService productInventoryService;

    public ProductInventoryDBUpdateRequest(ProductInventory productInventory, ProductInventoryService productInventoryService) {
        this.productInventory = productInventory;
        this.productInventoryService = productInventoryService;
    }


    /**
     * @param
     * @return void
     * @Description : 更新操作：1.先删除库存缓存 2.更新数据库
     * @Author : leizhang
     * @Date 16:28 2022/3/18
     **/
    @Override
    public void process() {
        try {
            //********1.清除缓存操作必须先于更新数据数据库******
            productInventoryService.removeProductInventoryCache(productInventory);
            productInventoryService.updateProductInventory(productInventory);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @Override
    public ProductInventory getProductInfo() {
        return productInventory;
    }

    @Override
    public boolean forceRefresh() {
        return false;
    }
}
